package main

import (
	"fmt"
	"strconv"
	"time"
	"tushare/model/mysql"
	"tushare/service/tushare"
	"tushare/service/word"

	"github.com/golang-module/carbon"
	"github.com/spf13/cobra"
)

func main() {
	mysql.InitDB()
	defer mysql.DB.Close()

	var cmdStockBasic = &cobra.Command{
		Use:   "stockBasic",
		Short: "股票列表",
		Long:  `拉取股票列表`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.StockBasic()
		},
	}

	var cmdMoneyflow = &cobra.Command{
		Use:   "moneyFlow",
		Short: "个股资金流向",
		Long:  `拉取个股资金流向数据 nohup ./cmd moneyFlow 20211105 20211105 &`,
		Run: func(cmd *cobra.Command, args []string) {
			var nowDate string
			if len(args) > 0 {
				nowDate = args[0]
			} else {
				//获取当前时间
				timeStr := carbon.Now().ToDateString()

				nowDateTime, _ := time.Parse("2006-01-02", timeStr)
				nowDate = nowDateTime.Format("20060102")
			}
			tushare.Moneyflow(nowDate, nowDate)
		},
	}

	var cmdDaily = &cobra.Command{
		Use:   "daily",
		Short: "daily",
		Long:  `nohup ./cmd daily 20211105 &`,
		Run: func(cmd *cobra.Command, args []string) {
			var nowDate string
			if len(args) > 0 {
				nowDate = args[0]
			} else {
				//获取当前时间
				timeStr := carbon.Now().ToDateString()

				nowDateTime, _ := time.Parse("2006-01-02", timeStr)
				nowDate = nowDateTime.Format("20060102")
			}
			tushare.Daily(nowDate)
		},
	}

	var cmdMinute = &cobra.Command{
		Use:   "minute",
		Short: "minute",
		Long:  `nohup ./cmd minute &`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.MinuteDay()
		},
	}

	var cmdMinuteAnalys = &cobra.Command{
		Use:   "minuteAnalys",
		Short: "minuteAnalys",
		Long:  `nohup ./cmd minuteAnalys &`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.MinuteAnalyse()
		},
	}

	var cmdWeekly = &cobra.Command{
		Use:   "weekly",
		Short: "weekly",
		Long:  `weekly`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.Weekly()
		},
	}

	var cmdRecentLowAmont = &cobra.Command{
		Use:   "recentLowAmont",
		Short: "近期低位",
		Long:  `扫描近期低位`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.RecentLowAmont()
		},
	}

	var cmdCheckMoneyflow = &cobra.Command{
		Use:   "checkMoneyFlow",
		Short: "校验个股资金流向",
		Long:  `校验个股资金流向数据是否缺失`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.CheckMoneyflow()

		},
	}

	var cmdFiveDayAvg = &cobra.Command{
		Use:   "fiveDayAvg",
		Short: "五日均线",
		Long:  `统计五日均线 nohup ./cmd fiveDayAvg &`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.FiveDayAvg()
		},
	}

	var cmdTushareRaisingLimit = &cobra.Command{
		Use:   "tushareRaisingLimit",
		Short: "连续涨停",
		Long:  `连续涨停统计`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.RaisingLimit()
		},
	}

	var cmdIm = &cobra.Command{
		Use:   "im",
		Short: "即时监控",
		Long:  `即时监控`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.ImStock()
			for {
				time.Sleep(time.Second * 10)
			}
		},
	}

	var cmdBackFive = &cobra.Command{
		Use:   "backFive",
		Short: "回踩五日均线",
		Long:  `统计回踩五日均线 nohup ./cmd backFive 20211105 &`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.BackFive(args[0])
		},
	}

	var cmdDayAngle = &cobra.Command{
		Use:   "dayAngle",
		Short: "日线角度",
		Long:  `统计日线角度 nohup ./cmd dayAngle 2021-11-05 60 &`,
		Run: func(cmd *cobra.Command, args []string) {
			avgDay := args[0]
			//获取当前时间
			timeStr := carbon.Now().ToDateString()
			nowDateTime, _ := time.Parse("2006-01-02", timeStr)
			tradeDate := nowDateTime.Format("2006-01-02")

			tushare.Angle(tradeDate, avgDay)
		},
	}

	var cmdImDayAngle = &cobra.Command{
		Use:   "imDayAngle",
		Short: "实时日线角度",
		Long:  `实时统计日线角度`,
		Run: func(cmd *cobra.Command, args []string) {
			//avgDay := args[0]
			tushare.ImAngle()
		},
	}

	var cmdMagicalNine = &cobra.Command{
		Use:   "magicalNine",
		Short: "神奇九转",
		Long:  `神奇九转`,
		Run: func(cmd *cobra.Command, args []string) {
			i, _ := strconv.ParseInt(args[0], 10, 64)
			typeNum, _ := strconv.ParseInt(args[1], 10, 64)
			startDate := args[2]
			tushare.MagicalNine(i, typeNum, startDate)
		},
	}

	var cmdSpiderStock = &cobra.Command{
		Use:   "spiderStock",
		Short: "SpiderStock",
		Long:  `SpiderStock`,
		Run: func(cmd *cobra.Command, args []string) {
			i, _ := strconv.ParseInt(args[0], 10, 64)
			tushare.SpiderStock(i)
		},
	}

	var cmdRedClampGreen = &cobra.Command{
		Use:   "redClampGreen",
		Short: "阳夹阴",
		Long:  `redClampGreen`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.RedClampGreen()
		},
	}

	var cmdSpiderStockByType = &cobra.Command{
		Use:   "spiderStockByType",
		Short: "spiderStockByType",
		Long:  `spiderStockByType`,
		Run: func(cmd *cobra.Command, args []string) {
			typeStr := args[0]
			for {
				tushare.SpiderStockByType(typeStr)
				time.Sleep(time.Second * 1)
			}
		},
	}

	var cmdElgVol = &cobra.Command{
		Use:   "elgVol",
		Short: "elgVol",
		Long:  `elgVol 统计超大的买入卖出占比`,
		Run: func(cmd *cobra.Command, args []string) {
			elgVol := new(tushare.ElgVol)
			elgVol.Elg(args)
		},
	}

	var cmdLine = &cobra.Command{
		Use:   "line",
		Short: "line",
		Long:  `line`,
		Run: func(cmd *cobra.Command, args []string) {
			line := new(tushare.Line)
			line.MinuteLine("SZ000009")
		},
	}

	var cmdDailyMin = &cobra.Command{
		Use:   "dailyMin",
		Short: "dailyMin",
		Long:  `获取一段时间内的最小值`,
		Run: func(cmd *cobra.Command, args []string) {
			limt, _ := strconv.ParseInt(args[0], 10, 64)
			tushare.DailyMinByLimit(limt)
		},
	}

	var cmdStatistics = &cobra.Command{
		Use:   "statistics",
		Short: "Statistics",
		Long:  `数据统计`,
		Run: func(cmd *cobra.Command, args []string) {
			typeStr := args[0]
			if typeStr == "openDay" {
				tushare.OpenDay()
			} else if typeStr == "lineAngle" {
				tushare.DailyLineAngle()
			} else if typeStr == "avgLineTop" {
				tushare.DailyAvgLineTop()
			} else if typeStr == "goldNeedle" {
				tushare.DailyGoldNeedle()
			} else if typeStr == "yjy" {
				tushare.DailyYjy()
			} else if typeStr == "ythreey" {
				tushare.DailyYthreey()
			} else if typeStr == "downFour" {
				tushare.DailyDownFour()
			}
		},
	}

	var cmdDailyCheck = &cobra.Command{
		Use:   "dialyCheck",
		Short: "dialyCheck",
		Long:  `dialyCheck`,
		Run: func(cmd *cobra.Command, args []string) {
			tushare.DailyCheck()
		},
	}

	var cmdWord = &cobra.Command{
		Use:   "word",
		Short: "word",
		Long:  `word`,
		Run: func(cmd *cobra.Command, args []string) {
			typeStr := args[0]
			if typeStr == "levelFour" {
				var i int64
				for i = 1; i <= 375; i++ {
					url := fmt.Sprintf("https://word.iciba.com/?action=words&class=16&course=%d", i)
					s := new(word.Spider)
					s.LevelFour(url)
				}

			} else if typeStr == "error-typo" {
				s := new(word.Spider)
				s.ErrorTypo()
			} else {
				letter := args[0]
				page, _ := strconv.ParseInt(args[1], 10, 64)
				s := new(word.Spider)
				s.Koolearn(letter, page)
			}
		},
	}

	var cmdAnalyse = &cobra.Command{
		Use:   "analyse",
		Short: "analyse",
		Long:  `analyse`,
		Run: func(cmd *cobra.Command, args []string) {
			typeStr := args[0]
			if typeStr == "percent" {
				a := new(tushare.Analyse)
				a.Percent()
			}
		},
	}

	var rootCmd = &cobra.Command{Use: "app"}
	rootCmd.AddCommand(cmdStockBasic)
	rootCmd.AddCommand(cmdMoneyflow)
	rootCmd.AddCommand(cmdDaily)
	rootCmd.AddCommand(cmdWeekly)
	rootCmd.AddCommand(cmdRecentLowAmont)
	rootCmd.AddCommand(cmdCheckMoneyflow)
	rootCmd.AddCommand(cmdFiveDayAvg)
	rootCmd.AddCommand(cmdTushareRaisingLimit)
	rootCmd.AddCommand(cmdIm)
	rootCmd.AddCommand(cmdBackFive)
	rootCmd.AddCommand(cmdDayAngle)
	rootCmd.AddCommand(cmdSpiderStock)
	rootCmd.AddCommand(cmdImDayAngle)
	rootCmd.AddCommand(cmdMagicalNine)
	rootCmd.AddCommand(cmdRedClampGreen)
	rootCmd.AddCommand(cmdSpiderStockByType)
	rootCmd.AddCommand(cmdElgVol)
	rootCmd.AddCommand(cmdLine)
	rootCmd.AddCommand(cmdDailyMin)
	rootCmd.AddCommand(cmdStatistics)
	rootCmd.AddCommand(cmdDailyCheck)
	rootCmd.AddCommand(cmdMinute)
	rootCmd.AddCommand(cmdMinuteAnalys)
	rootCmd.AddCommand(cmdAnalyse)

	rootCmd.AddCommand(cmdWord)
	rootCmd.Execute()
}
